var Api = require("../../../api/method.js");
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentData: 0,
    columnid: "",
    goodsList: "",
    isBuy: "",
    price: "",
    src: '',
    pop: true,
    system:""
  },
  //获取当前滑块的index
  bindchange: function (e) {
    const that = this;
    that.setData({
      currentData: e.detail.current
    })
  },
  //点击切换，滑块index赋值
  checkCurrent: function (e) {
    const that = this;

    if (that.data.currentData === e.target.dataset.current) {
      return false;
    } else {

      that.setData({
        currentData: e.target.dataset.current
      })
    }
  },
  // 专栏详情
  columnDetail: function (e) {
    let that = this;
    let list = {
      columnId: that.data.columnid
    };
    Api.columnDetail(list, function (err, res) {
      console.log(res.data);
      if (res.data.code == 1) {
        that.setData({
          imgUrl: res.data.data.imgUrl,
          name: res.data.data.name,
          content: res.data.data.content,
          goodsList: res.data.data.goodsList,
          name: res.data.data.name,
          isBuy: res.data.data.isBuy,
          price: res.data.data.price
        })
      }
    });
  },
  //确认支付
  pay: function (e) {
    let that = this;
    if (this.data.system == "iOS") {
      wx.showToast({
        title: "设备暂不支持",
        icon: "fail",
        duration: 1500
      });
    }else{
      let list = {
        price: that.data.price * 100,
        goodsId: that.data.columnid
      };
      console.log(list);
      Api.Reqpay(list, function (err, res) {
        console.log(res.data);
        if (res.data.code == 1) {
          wx.requestPayment({
            timeStamp: res.data.data.timeStamp,
            nonceStr: res.data.data.nonceStr,
            package: res.data.data.package,
            signType: res.data.data.signType,
            paySign: res.data.data.sign,
            success(res) {
              var user = wx.getStorageSync("user");
              user.roleCode = "user"
              wx.setStorageSync("user", user);
              wx.redirectTo({
                url: '/pages/Salesman/curriculum/curriculum',
              })
            },
            fail(res) {
              wx.showToast({
                title: "支付取消",
                icon: "fail",
                duration: 1500
              })
            }
          })
        }
      }); 
    }
  },
  //获取设备型号
  getSystemInfo:function(e){
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        // console.log(res)
        var system = res.system.replace(/[^a-z]+/ig, "");
        that.setData({
          system: system
        })
      }
    });
  },
  //点击图片二维码
  dada: function (e) {
    console.log(e)
    var img = [];
    img.push(e.currentTarget.dataset.src);
    wx.previewImage({
      current: e.currentTarget.dataset.src,
      urls: img
    })
  },
  //隐藏白条弹框
  hideBaitiaoView: function (e) {
console.log(e)
    if (e.target.dataset.target == 'self')
      this.setData({
        pop: true
      })
  },
  //去学习
  gostart: function () {
    let that = this;
    let list = {
      columnId: that.data.columnid
    };
    Api.columnDetail(list, function (err, res) {
      console.log(res.data);
      if (res.data.code == 1) {
        if (res.data.data.redirectUrl) {
          that.setData({
            src: res.data.data.redirectUrl,
            pop: false
          })
        }

      }
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options);
    this.setData({
      columnid: options.columnid
    })
    this.columnDetail();
    this.getSystemInfo();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})